package com.example.homework.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.homework.ContactArrayAdapter;
import com.example.homework.ContactInfoActivity;
import com.example.homework.CreateContactActivity;
import com.example.homework.DBHelper;
import com.example.homework.MainActivity;
import com.example.homework.R;
import com.example.homework.model.Contact;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;
import java.util.stream.Collectors;

public class ContactsFragment extends Fragment {
    private FloatingActionButton floatingActionButton;
    private ListView listView;
    private ContactArrayAdapter contactArrayAdapter;
    private ActionMode actionMode;
    private Contact selectedContact;
    private DBHelper dbHelper;
    private ActionMode.Callback callback=new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater=mode.getMenuInflater();
            inflater.inflate(R.menu.contact_action_menu,menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            boolean changed=false;
            if (item.getItemId()==R.id.block_menu){
                dbHelper.updateBlock(dbHelper.getWritableDatabase(),selectedContact.getId(),true);
                changed=true;
            }
            if (item.getItemId()==R.id.del_menu){
                dbHelper.del(dbHelper.getWritableDatabase(),selectedContact.getId());
                changed=true;
            }
            if (changed){
                contactArrayAdapter.remove(selectedContact);
                contactArrayAdapter.notifyDataSetChanged();
            }
            actionMode.finish();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            actionMode=null;
            selectedContact=null;
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contacts_layout, container, false);
        this.floatingActionButton = view.findViewById(R.id.float_create_button);
        floatingActionButton.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), CreateContactActivity.class);
            startActivity(intent);
        });
        this.dbHelper=new DBHelper(getContext());
        this.listView=view.findViewById(R.id.contact_list);
        contactArrayAdapter=new ContactArrayAdapter(this.getContext(),R.layout.contact_info_item_layout);
        contactArrayAdapter.setContacts(dbHelper.queryAll(dbHelper.getReadableDatabase()));
        this.listView.setAdapter(contactArrayAdapter);
        listView.setOnItemLongClickListener((AdapterView<?> parent, View v, int position, long id)->{
            if (actionMode!=null){
                return false;
            }
            actionMode= getActivity().startActionMode(callback);
            v.setSelected(true);
            selectedContact=contactArrayAdapter.getItem(position);
            return true;
        });
        listView.setOnItemClickListener((parent,v,position,id)->{
            Intent intent=new Intent(getContext(), ContactInfoActivity.class);
            Contact contact=contactArrayAdapter.getItem(position);
            intent.putExtra("firstname",contact.getFirstName());
            intent.putExtra("lastname",contact.getLastName());
            intent.putExtra("phone",contact.getPhone());
            startActivity(intent);
        });
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        MainActivity mainActivity=(MainActivity) getActivity();
        List<Contact> contacts=mainActivity.dbHelper.queryAll(mainActivity.dbHelper.getReadableDatabase())
                        .stream().filter(contact -> {
                            return !contact.isBlocked();
                }).collect(Collectors.toList());
        contactArrayAdapter.clear();
        contactArrayAdapter.setContacts(contacts);
    }
}
